const { EventEmitter } = require('events');
// 实现继承
const { inherits } = require('util');


function Animal (type) {
  this.type = type;
}

// Animal继承EventEmitter类，拥有EventEmitter类的所有属性隔和方法
inherits(Animal, EventEmitter);

const dog = new Animal('dog');

const cat = new Animal('cat');

// 实现订阅
[dog, cat].forEach(item => {
  item.on('say', msg => console.log(`the ${item.type} says ${msg}`));
})

// 实现发布
dog.emit('say', 'bark');
cat.emit('say', 'miao');





